package com.chan.service.impl;

import com.chan.model.Demo;
import com.chan.repository.DemoRepository;
import com.chan.service.DemoService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Created by Administrator on 2016/9/23.
 */
@Service
public class DemoServiceImpl implements DemoService {
    @Resource
    private DemoRepository demoRepository;

    @Resource
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public void test() {
        ValueOperations<String, String> ops = redisTemplate.opsForValue();
        ops.set("mykey","random="+Math.random());
        System.out.println("mykey::"+ops.get("mykey"));
    }

    @Cacheable(value = "demoInfo")
    @Override
    public Demo findById(Long id) {
        System.err.println("---------查询数据库获取数据id:" + id + "-----------------");
        return demoRepository.findOne(id);
    }


    @CacheEvict(value = "demoInfo")
    @Override
    public void deleteFromCache(Long id) {
    }
    @CacheEvict(value="demoInfo",allEntries = true)
    @Override
    public void deleteAllCache(){
    }
}
